﻿#if NET6_0_OR_GREATER
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using T.ServiceFramework.Service;
//using System.Web.Http;

namespace T.SF.Console.HttpHost
{
    [WebApiExceptionFilter]
    [Route("api/invoke")]
    public class InvokeController : ControllerBase
    {
        [HttpGet]
        [AllowAnonymous]
        public async Task<object> Get()
        {
            return await WebHostService.Current.Invoker.InvokeAsync(base.HttpContext, null);
        }

        // POST
        [HttpPost]
        [HttpPut]
        [HttpDelete]
        [HttpHead]
        [HttpOptions]
        [HttpPatch]
        [AllowAnonymous]
        public async Task<object> Post(Dictionary<string, string> data)
        {
            return await WebHostService.Current.Invoker.InvokeAsync(base.HttpContext, data);
        }

        // POST
        [HttpPost]
        [HttpPut]
        [HttpDelete]
        [HttpHead]
        [HttpOptions]
        [HttpPatch]
        [AllowAnonymous]
        [Consumes("application/json")]
        public async Task<object> Post([FromBody] Hashtable data)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            if (data != null && data.Count > 0)
            {
                foreach (string key in data.Keys)
                {
                    dict[key] = Convert.ToString(data[key]);
                }
            }

            return await WebHostService.Current.Invoker.InvokeAsync(base.HttpContext, dict);
        }
    }
}
#endif